package com.chenzhengrong.demospringboot.common.base;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

/**
 * 分页封装.
 *
 * @author M1nG
 * @date 2021/06/05  8:40
 */
@Data
public class PageInfo<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long pageNum;// 当前页码
    private Long pageSize;// 一页多少条
    private long total;// 总记录数
    private List<T> records;// 记录

    public PageInfo(Long pageNum, Long pageSize, int draw) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public PageInfo(Long pageNum, Long pageSize, long total, List<T> records) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
        this.records = records;
    }


    public PageInfo(Page<T> page) {
        this.pageNum = page.getCurrent();
        this.pageSize = page.getSize();
        this.total = page.getTotal();
        this.records = page.getRecords();
    }

    public PageInfo() {

    }

    public Page getPage() {
        return new Page(this.pageNum, this.pageSize);
    }

    public static Page createPageInfo(Integer current, Integer size) {
        Integer OptCurrent = Integer.valueOf(Optional.ofNullable(current).orElse(1));
        Integer OptSize = Integer.valueOf(Optional.ofNullable(size).orElse(10));
        return new Page(OptCurrent.longValue(), OptSize.longValue());
    }

}